<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-20 12:53:55
 */

namespace app\admin\controller;
use app\common\controller\Backend;

class Config extends Backend {

	private $config_space = 'system';

	function initialize() {
		parent::initialize();
		$this->saveAndGetConfigIdentify('', '系统配置', '', 'config');

		$this->configDao = model('common/Config');
	}

	public function index() {
		$info = $this->configDao->getConfigsBySpace($this->config_space);
		//客服二维码
		$ftpWeb = config('app.ftp_web');
		$info['comp_ewm'] = $info['comp_ewm'] ? $ftpWeb.$info['comp_ewm'] : '';
		$info['kefu_ewm'] = $info['kefu_ewm'] ? $ftpWeb.$info['kefu_ewm'] : '';
		$info['simple_title'] = $info['simple_title'] ? $info['simple_title'] : 'DancePHP';
		$this->assign('info', $info);

		//获取微信公众号列表
		$weixinAccountDao = model('weixin_manage/WeixinAccount');
		list($weixinAccountList) = $weixinAccountDao->search([]);
		$this->assign('weixinAccountList', $weixinAccountList);

		//获取微信小程序列表
		$weixinAppletDao = model('weixin_manage/WeixinApplet');
		list($weixinAppletList) = $weixinAppletDao->search([]);
		$this->assign('weixinAppletList', $weixinAppletList);

		return $this->fetch();
	}

	public function doSet() {
		$data = input('post.data/a', '', 'pwEscape');
		//print_r($data);exit;

		$info = $this->configDao->getConfigsBySpace($this->config_space);

		//处理编辑器内容
		//$data['site_seo_description'] = ueditorContent($data['site_seo_description']);
		
		//省市选择
		$userProvinceId = input('post.userProvinceId', '', 'pwEscape');
		$userCityId = input('post.userCityId', '', 'pwEscape');
		$userDistrictId = input('post.userDistrictId', '', 'pwEscape');
		$data['province'] = $userProvinceId;
		$data['city'] = $userCityId;
		$data['district'] = $userDistrictId;

		//平台二维码
		list($code, $msg, $uploadInfo) = $this->comuploadFile('config', 'comp_ewm', $info['comp_ewm']);
		$uploadInfo['saveFullPath'] && $data['comp_ewm'] = $uploadInfo['saveFullPath'];
		
		//客服二维码
		list($code, $msg, $uploadInfo) = $this->comuploadFile('config', 'kefu_ewm', $info['kefu_ewm']);
		$uploadInfo['saveFullPath'] && $data['kefu_ewm'] = $uploadInfo['saveFullPath'];
		
		//分享图标
		list($code, $msg, $uploadInfo) = $this->comuploadFile('config', 'share_icon', $info['share_icon']);
		$uploadInfo['saveFullPath'] && $data['share_icon'] = $uploadInfo['saveFullPath'];

		foreach ($data as $key => $value) {
			$checkInfo = $this->configDao->getConfigByNameAndSpace($key, $this->config_space);
			$sqlData = [
				'config_value'	=> $value,
			];
			if ($checkInfo) {
				$this->configDao->updateConfigByNameAndSpace($key, $this->config_space, $sqlData);
			} else {
				$sqlData['config_space'] = $this->config_space;
				$sqlData['config_name'] = $key;
				$sqlData['description'] = '';
				$this->configDao->insert($sqlData);
			}
		}
		$sys_identify = 'system_config';
		cache($sys_identify, NULL); //删除缓存数据

		$this->success('保存成功');
	}

}